package com.yang.mall.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.mall.dto.BannerDTO;
import com.yang.mall.entity.Banner;
import com.yang.mall.entity.QueryRequest;
import com.yang.mall.mapper.BannerMapper;
import com.yang.mall.service.IBannerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 广告表 服务实现类
 * </p>
 *
 * @author kadorlin
 * @since 2022-10-21
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {
    @Resource
    BannerMapper bannerMapper;


    @Override
    public void add(Banner banner){
        this.save(banner);
    }

    @Override
    public List<Banner> getListByBannerDTO(BannerDTO bannerDTO){

        return bannerMapper.getListByBannerDTO(bannerDTO);
    }

    @Override
    public List<Banner> getDisplayedByBannerDTO(BannerDTO bannerDTO){
        bannerDTO.setStatus(1);
        return bannerMapper.getListByBannerDTO(bannerDTO);
    }

    @Override
    public IPage<Banner> getPageByBannerDTO(QueryRequest request,BannerDTO bannerDTO){
        Page<Banner> page = new Page<>(request.getPageNum(), request.getPageSize());

        return bannerMapper.getPageByBannerDTO(page,bannerDTO);
    }

    @Override
    public void update(Banner banner){
        this.updateById(banner);
    }


    @Override
    public void delete(List<Long> ids){

        List<Banner> banners = new ArrayList<>(ids.size());
        ids.forEach(item->{
            Banner banner =new Banner();
            banner.setBannerId(item);
            banner.setDelFlag("1");
            banner.setUpdateTime(LocalDateTime.now());
            banners.add(banner);
        });

        this.updateBatchById(banners);
    }
}
